import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import compressPlugin from 'vite-plugin-compression'
import autoImport from 'unplugin-auto-import/vite'
import components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import basicSsl from '@vitejs/plugin-basic-ssl'
import vueJsx from '@vitejs/plugin-vue-jsx'

const target: string = 'https://test.greatgas.cn/' // 测试环境

// 需要注入的样式配置
// const importScssList = ['@use "@/scss/mixins.scss" as *;']
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const isProduction = mode === 'production'

  return {
    // base: isProduction ? '/v3/trad' : '/',
    server: {
      https: false,
      proxy: {
        '^/new': {
          target: target,
          changeOrigin: true,
          secure: false,
        },
      },
    },
    build: {
      rollupOptions: {
        input: 'index.ts',
        output: [
          {
            format: 'es', // 打包格式
            entryFileNames: '[name].mjs', // 打包后文件名
            preserveModules: true, // 让打包目录和我们目录对应
            dir: 'es', // 配置打包根目录
          },
          {
            format: 'cjs', // 打包格式
            entryFileNames: '[name].js', // 打包后文件名
            preserveModules: true, // 让打包目录和我们目录对应
            dir: 'lib', // 配置打包根目录
          },
        ],
        external: [
          /node_modules/,
          '@enn/ency-design',
          '@enn/ency-design-icons',
          '@ennew/greatgas-use-navigater',
          '@wangeditor/editor',
          '@wangeditor/editor-for-vue',
          '@wangeditor/editor/dist/css/style.css',
          'axios',
          'dayjs',
          'js-base64',
          'js-md5',
          'lodash-es',
          'pinia',
          'qs',
          'vue',
          'vue-router',
        ],
      },
      lib: {
        entry: 'index.ts',
        name: 'common-vue3',
        fileName: 'common-vue3',
        formats: ['es', 'umd', 'cjs'],
      },
    },
    css: {
      // preprocessorOptions: {
      //   scss: { additionalData: importScssList.join('') },
      // },
    },
    plugins: [
      vue(),
      vueJsx(),
      //访问https
      basicSsl(),
      // 自动导入 
      autoImport({
        imports: ['vue', 'vue-router', 'pinia'],
        dts: 'src/auto-import.d.ts',
        resolvers: [ElementPlusResolver({ importStyle: 'sass' })],
      }),
      // 组件自动导入
      components({
        dts: 'src/components.d.ts',
        resolvers: [ElementPlusResolver({ importStyle: 'sass' })],
      }),
      // 压缩
      compressPlugin({
        verbose: true, // 默认即可
        disable: false, // 开启压缩(不禁用)，默认即可
        deleteOriginFile: false, // 删除源文件
        threshold: 10240, // 压缩前最小文件大小
        algorithm: 'gzip', // 压缩算法
        ext: '.gz', // 文件类型
      }),
      // mode === 'production'
      // ? postcss({ extract: 'index.css', plugins: [autoprefixer()] })
      // : undefined,
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
      },
    },
  }
})

